﻿using AutoMapper;
using MediatR;
using MyMES.Domain.yjb;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.settingCommand;
using MyMES.Read.API.FileNlog;

namespace MyMES.Read.API.Application.CommendHandlers.settingCommand
{
    public class TaskInfoShowCommandHandlers : IRequestHandler<TaskInfoShowCommand, APIPaging<List<TaskInfo>>>
    {
        private readonly IBaseRepository<TaskInfo> accreop;
        private readonly IMapper mapper;

        public TaskInfoShowCommandHandlers(IBaseRepository<TaskInfo> accreop, IMapper mapper)
        {
            this.accreop = accreop;
            this.mapper = mapper;
        }
        /// <summary>
        /// 生产显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIPaging<List<TaskInfo>>> Handle(TaskInfoShowCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIPaging<List<TaskInfo>> result = new APIPaging<List<TaskInfo>>();
                result.Code = APIEnum.查询失败;
                result.Msg = "查询成功";
                string sql = "SELECT * FROM TaskInfo JOIN workorderinfo ON TaskInfo.TaskInfowjId = workorderinfo.WorkOrderInfoId WHERE TaskInfo.IsDelete = false ORDER BY TaskInfo.TaskInfoId DESC";
                var list = await accreop.SelectAsync(sql);
                result.TotalCount = list.Count;
                result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.Size);
                list = list.Skip((request.Index - 1) * request.Size).Take(request.Size).ToList();
                result.data = list;
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
